package app.web.admin.mapper;

import app.web.admin.domain.pojo.AdminRole;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryChain;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * 角色 Mapper
 * @author feizns
 * @since 2025-04-09
 */
@Mapper
public interface AdminRoleMapper extends BaseMapper<AdminRole> {

    /**
     * 存在
     * @param name 名字
     * @param groupName 组名
     * @return boolean
     */
    static boolean exists(String name, String groupName) {
        return new AdminRole().where(AdminRole::getName).eq(name).where(AdminRole::getGroupName).eq(groupName).exists();
    }

    /**
     * 查找指定角色的所有菜单编号
     * @param roleIds 角色编号列表
     * @return {@link List }<{@link Long }>
     */
    static List<Long> findMenuIdsByIds(List<Long> roleIds) {
        //noinspection unchecked
        return CollectionUtils.isEmpty(roleIds)
                ? Collections.emptyList()
                : QueryChain.of(AdminRole.class).in(AdminRole::getId, roleIds).select(AdminRole::getMenuIds).list()
                .stream().map(AdminRole::getMenuIds).flatMap(List::stream).distinct().toList();
    }

    /**
     * 超级管理员角色
     * @return {@link AdminRole }
     */
    static AdminRole superAdminRole() {
        return QueryChain.of(AdminRole.class).where(AdminRole::getId).eq(AdminRole.SuperAdminRoleId).one();
    }

    /**
     * 找到根据名字和群编号
     * @param name 名字
     * @param groupId 组编号
     * @return {@link AdminRole }
     */
    AdminRole findByNameAndGroupId(String name, Long groupId);

}
